// pages/shopcart/shopcart.js
import Dialog from '@vant/weapp/dialog/dialog'
Component({
  data: {
    allSelected: false,
    isEdit: false,
    list: [],
    selectProductList: [],
    selectArr: [],
    totalPrice: 0
  },
  observers: {
    'selectProductList.length,selectProductList'(length, selectProductList) {
      this.setData({
        totalPrice: this.data.selectProductList.reduce((pre, next) => {
          return pre + next.goods_price * next.num
        }, 0),
        allSelected: length === this.data.list.length,
        selectArr: this.data.selectProductList.map((item) => '' + item.goods_id)
      })
    },
    list(list) {
      const selectProductList = [...this.data.selectProductList]
      const restId = []
      list.forEach((item) => {
        const index = selectProductList.findIndex(
          (productItem) => productItem.goods_id === item.goods_id
        )
        restId.push(item.goods_id)
        if (index > -1) {
          selectProductList[index] = item
        }
      })
      this.setData({
        selectProductList: selectProductList.filter((item) =>
          restId.includes(item.goods_id)
        )
      })
      wx.setStorageSync('shopcart', list)
    }
  },
  methods: {
    onShow() {
      const list = wx.getStorageSync('shopcart')
      this.setData({
        list
      })
    },
    onEditClick() {
      this.setData({
        isEdit: !this.data.isEdit
      })
    },
    onCatchTap(e) {
      // do nothing
    },
    onStepChange(e) {
      const item = e.currentTarget.dataset.item
      const index = this.data.list.findIndex(
        (productItem) => productItem.goods_id === item.goods_id
      )
      const list = [...this.data.list]
      list[index] = {
        ...this.data.list[index],
        num: e.detail
      }
      this.setData({
        list
      })
    },
    onItemChange(e) {
      const item = e.currentTarget.dataset.item
      const selectProductList = [...this.data.selectProductList]
      const index = selectProductList.findIndex(
        (productItem) => productItem.goods_id === item.goods_id
      )
      if (index > -1) {
        selectProductList.splice(index, 1)
      } else {
        selectProductList.push(item)
      }
      this.setData({
        selectProductList
      })
    },
    onAllSelectedAllChange() {
      this.setData({
        selectProductList: !this.data.allSelected ? this.data.list : []
      })
    },
    onDeleteItem(e) {
      const item = e.currentTarget.dataset.item
      Dialog.confirm({
          title: '提示',
          message: `确定要删除 ${item.goods_name} 吗？`
        })
        .then(() => {
          const list = this.data.list.filter(
            (productItem) => productItem.goods_id !== item.goods_id
          )
          this.setData({
            list
          })
        })
        .catch(() => {})
    },
    onSubmitOrder() {
      if (!this.data.isEdit) {
        const detail = encodeURIComponent(
          JSON.stringify(this.data.selectProductList)
        )
        wx.navigateTo({
          url: `/pages/pay/pay?detail=${detail}`
        })
      } else {
        Dialog.confirm({
            title: '提示',
            message: '确定要删除选中项吗？'
          })
          .then(() => {
            const list = this.data.list.filter((item) => {
              if (
                this.data.selectProductList.find(
                  (productItem) => productItem.goods_id === item.goods_id
                )
              ) {
                return false
              }
              return true
            })
            this.setData({
              list
            })
          })
          .catch(() => {})
      }
    }
  }
})